use regex::Regex;
use std::str;

pub async fn get_huya_real_url(room_id: &str) {
    let room_url = format!("https://m.huya.com/{}", room_id);

    let mut default_header = reqwest::header::HeaderMap::new();
    default_header.insert(
        "User-Agent",
        reqwest::header::HeaderValue::from_static(concat!(
            "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) ",
            "AppleWebKit/537.36 (KHTML, like Gecko) ",
            "Chrome/75.0.3770.100 Mobile Safari/537.36"
        )),
    );

    let client = reqwest::Client::builder()
        .default_headers(default_header)
        .build()
        .expect("build request client Error");

    let texts = client
        .get(room_url)
        .send()
        .await
        .expect("Failed to send req")
        .text_with_charset("utf-8")
        .await
        .expect("get text error");

    let match_liveurl_re = Regex::new(r#"liveLineUrl = "([\s\S]*?)";"#).unwrap();
    let livelineurl_matched = match_liveurl_re
        .captures(texts.as_str())
        .unwrap()
        .get(1)
        .expect("faile get a result")
        .as_str();
    let livelineurl_b = base64::decode(livelineurl_matched).expect("decode base64 error");
    let livelineurl = str::from_utf8(&livelineurl_b).unwrap();
    println!("live_url: [http:{}]", livelineurl);
}
